import Vue from 'vue'
import store from './store'
import App from './App'
import commomMixin from './mixins/common'

const showToast = function (title, data = {}) {
    if (Boolean(title) === false) {
        return;
    }
    const baseData = {
        duration: 1500,
        mask: false,
        icon: 'none'
    }

    data = Object.assign({}, baseData, data);
    console.log('data', data);
    //统一提示方便全局修改
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            uni.showToast({
                title,
                duration: data.duration,
                mask: data.mask,
                icon: data.icon,
                success: (res) => {
                    setTimeout(() => {
                        resolve(res)
                    }, data.duration);
                },
                fail: (error) => {
                    setTimeout(() => {
                        reject(error)
                    }, data.duration);
                }
            });
        }, 0);
    })

}

const showModal = function (data = {}) {
    //统一提示方便全局修改
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            uni.showModal({
                title: data.title || '提示',
                content: data.content || '是否确认操作？',
                showCancel: data.showCancel || false,
                cancelText: data.cancelText || '取消',
                confirmText: data.confirmText || '确定',
                success: res => {
                    // resolve(res)
                    if (res.confirm) {
                        resolve(res)
                    } else {
                        reject({
                            resultInfo: '操作已取消'
                        })
                    }
                },
                fail: error => {
                    reject(error)
                }
            });
        }, 0);
    })
}



const prePage = () => {
    let pages = getCurrentPages();
    let prePage = pages[pages.length - 2];
    // #ifdef H5
    return prePage;
    // #endif
    return prePage.$vm;
}

const platform = () => {
    let code = '';
    let provider = '';
    // #ifdef MP-WEIXIN
    code = '1102';
    provider = 'weixin';
    // #endif
    return {
        code,
        provider
    }
}

Vue.config.productionTip = false
Vue.prototype.$fire = new Vue();
Vue.prototype.$store = store;
Vue.prototype.$api = {
    showToast,
    showModal,
    prePage,
    platform
};


Vue.mixin(commomMixin)
App.mpType = 'app'

const app = new Vue({
    ...App
})

app.$mount()

/* 
    "localApiUri":"https://bdance.csgxcf.com/vimi/",
*/